package com.sunny.study.api.controller;

import com.sunny.study.api.pojo.Result;
import com.sunny.study.api.service.FileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * @ClassName: FileController
 * @Description:
 * @Author: sunt
 * @Date: 2020/5/20 13:59
 * @Version 1.0
 **/
@RestController
@RequestMapping("file")
public class FileController {


    @Autowired
    private FileService fileService;

    /**
     * 文件上传
     * @param file
     * @return
     */
    @PostMapping("upload")
    public Result uploadFile(@RequestParam(value = "file") MultipartFile file) {
        try {
            String upload = fileService.upload(file.getBytes());
            return Result.success(upload);
        } catch (IOException e) {
            e.printStackTrace();
            return Result.fail("上传异常:" + e.getMessage());
        }
    }
}
